package com.example.demo.controller;

import com.example.demo.service.SysMenuService;
import com.example.demo.dao.Creation;
import com.example.demo.domain.SysMenu;
import com.example.demo.controller.BaseController;
import com.example.demo.datatable.PageParameter;
import com.example.demo.datatable.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

import static com.example.demo.dao.Creation.Operation.eq;

@Controller
@RequestMapping("/sysMenu")
public class SysMenuController extends BaseController{
    @Autowired
    private SysMenuService sysMenuService;

    @GetMapping("/addView")
    public String addView(ModelMap modelMap){
        List<SysMenu> list = sysMenuService.search(new Creation("level", eq, 1));
        modelMap.addAttribute("list", list);
        return "buss/sysMenu/add";
    }

    @GetMapping("/listView")
    public String listView(){
        return "buss/sysMenu/list";
    }

    @GetMapping("/updateView")
    public String updateView(String id, ModelMap modelMap){
        modelMap.addAttribute("id", id);
        List<SysMenu> list = sysMenuService.search(new Creation("level", eq, 1));
        modelMap.addAttribute("list", list);
        return "buss/sysMenu/add";
    };

    @GetMapping
    @ResponseBody
    public List list(HttpServletRequest request){
        return sysMenuService.grid();
    }

    @ResponseBody
    @PostMapping
    public ResponseEntity save(@RequestBody SysMenu sysMenu){
        if(StringUtils.isEmpty(sysMenu.getId())){
            if(StringUtils.isEmpty(sysMenu.getParentId())){
                sysMenu.setLevel(1);
            }else{
                sysMenu.setLevel(2);
            }
            sysMenuService.insert(sysMenu);
        }else{
            if(StringUtils.isEmpty(sysMenu.getParentId())){
                sysMenu.setLevel(1);
            }else{
                sysMenu.setLevel(2);
            }
            sysMenuService.update(sysMenu);
        }
        return ResponseEntity.ok("success");
    }

    @GetMapping("/{id}")
    @ResponseBody
    public ResponseEntity get(@PathVariable String id){
        SysMenu entity = sysMenuService.get(id);
        return ResponseEntity.ok(entity);
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    public ResponseEntity delete(@PathVariable String id){
        sysMenuService.delete(id);
        return ResponseEntity.ok("success");
    }

    @DeleteMapping
    @ResponseBody
    public ResponseEntity batchDelete(String ids){
        Arrays.asList(ids.split(",")).forEach(sysMenuService::delete);
        return ResponseEntity.ok("success");
    }
}
